import {
  Allow,
  IsArray, IsBoolean,
  IsDate,
  IsEmail,
  IsNotEmpty,
  IsNumber,
  IsOptional,
  IsString,
  Length,
  Matches
} from "class-validator";
import { Optional } from "@nestjs/common";
import { Exclude } from "class-transformer";

export class RegisterUserDto {

  @IsString()
  @Length(6, 20, { message: `密码长度必须大于$constraint1到$constraint2之间` })
  @IsNotEmpty({ message: '密码不能为空' })
  password: string;

  @IsEmail({}, {
    message: '不是合法的邮箱格式'
  })
  @IsNotEmpty({
    message: '邮箱不能为空'
  })
  email: string;

  @IsNotEmpty({
    message: '验证码不能为空'
  })
  captcha: string;
}

export class LoginUserDto {

  @IsEmail({}, {
    message: '不是合法的邮箱格式'
  })
  @IsNotEmpty({
    message: "邮箱不能为空"
  })
  email: string;

  @IsString()
  @Length(6, 20, { message: `密码长度必须大于$constraint1到$constraint2之间` })
  @IsNotEmpty({ message: '密码不能为空' })
  password: string;
}


export class UpdateUserPasswordDto {
  @IsString()
  @Length(6, 20, { message: `密码长度必须大于$constraint1到$constraint2之间` })
  @IsNotEmpty({
    message: '密码不能为空'
  })
  password: string;

  @IsEmail({}, {
    message: '不是合法的邮箱格式'
  })
  @IsNotEmpty({
    message: '邮箱不能为空'
  })
  email: string;

  @IsNotEmpty({
    message: '验证码不能为空'
  })
  captcha: string;
}

export class UpdateUserDto {

  @Exclude()
  password?: string;

  @IsString()
  @IsOptional()
  headPic?: string;

  @IsString()
  @IsNotEmpty({ message: '姓名不能为空' })
  @IsOptional()
  username?: string;

  // 昵称
  @IsString()
  @IsOptional()
  nickName?: string;

  // 性别
  @IsNumber()
  @IsOptional()
  gender?: number;

  // 出生日期
  @IsOptional()
  birthdayDate?: Date;

  @IsOptional()
  @IsArray()
  roleIds?: number[];

  //状态
  @IsBoolean()
  @IsOptional()
  status?: boolean;
}

export class GetUserDto {
  @Allow()
  pageSize?: number;

  @Allow()
  pageNo?: number;

  // 根据姓名查询
  @Allow()
  username?: string;

  // 根据性别查询
  @Allow()
  gender?: number;

  // 根据角色查询
  @Allow()
  role?: number;

  // 状态
  @Allow()
  status?: boolean;
}

export class AddUserRolesDto {
  @IsArray()
  roleIds: number[];
}

export class CreateUserDto{
  //邮箱
  @IsEmail({}, {
    message: '不是合法的邮箱格式'
  })
  @IsNotEmpty({
    message: '邮箱不能为空'
  })
  email: string;


  @IsString()
  @IsNotEmpty({ message: '姓名不能为空' })
  username: string;

  // 昵称
  @IsString()
  @IsNotEmpty({ message: '昵称不能为空' })
  nickName: string;

  // 性别
  @IsNumber()
  @IsNotEmpty({ message: '性别不能为空'})
  gender: number;

  //手机号
  @IsString()
  @IsNotEmpty({ message: '手机号不能为空' })
  @Matches(/^(?:(?:\+|00)86)?1[3-9]\d{9}$/, { message: '手机号码格式不正确' })
  phoneNumber: string;

  //密码
  @IsString()
  @IsNotEmpty({ message: '密码不能为空' })
  @Length(6, 20, { message: `密码长度必须大于$constraint1到$constraint2之间` })
  password: string;
}

export class UpdatePasswordDto {
  @IsString({ message: '密码必须是字符串' })
  @IsNotEmpty({ message: '密码不能为空' })
  @Length(6, 20, { message: `密码长度必须大于$constraint1到$constraint2之间` })
  password: string;
}

